package com.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mapper.JiLuMapper;
import com.mapper.TaocanMapper;
import com.mapper.UserMapper;
import com.pojo.JiLu;
import com.pojo.Taocan;
import com.pojo.User;
import com.service.UserService;

@Service
public class UserServiceImpl implements UserService{
	@Autowired
    private UserMapper userMapper;
	//加 更换套餐需要
	@Autowired
	private TaocanMapper taocanMapper;
	//加 统计金额需要
	@Autowired
	private JiLuMapper jiluMapper;
	
    @Override
    public User login(String username, String password) {
        return userMapper.login(username,password);
    }

    @Override
    public int register(User user) {
        return userMapper.register(user);
    }
    
    //加 更换套餐需要
	@Override
	public User getUserByCardNumber(String cardNumber) {
		// TODO Auto-generated method stub
		return userMapper.getUserByCardNumber(cardNumber);
	}
	//判断余额是否够更换套餐的需要
	@Override
	public boolean changeTaoCan(String cardNumber, String password, int newTaocanId) {
		// TODO Auto-generated method stub
		User user=getUserByCardNumber(cardNumber);
		if(user!=null&&user.getPassword().equals(password)) {
			Taocan newTaoCan=taocanMapper.getTaocanById(newTaocanId);
			if(user.getMoney()>=newTaoCan.getYuezifei()) {
				user.setTaocan(newTaocanId);
				user.setMoney(user.getMoney()-newTaoCan.getYuezifei());
				userMapper.updateUser(user);
				return true;
			}
		}
		return false;
	}
//当月消费金额重新统计
	@Override
	public int recalculateConsumption(String cardNumber, int newTaocanId) {
		// TODO Auto-generated method stub
		User user=getUserByCardNumber(cardNumber);
		if(user==null) {
			return 0;
		}
		Taocan newTaocan=taocanMapper.getTaocanById(newTaocanId);
		
		Map<String,Object> paramMap=new HashMap<>();
		paramMap.put("cardNumber", cardNumber);
		paramMap.put("i",newTaocanId);
		List<JiLu> jiluList=jiluMapper.getjiluByUsercardAndTaocanId(paramMap);
		
		double totalConsumption=0;
		double liuliangFee=0;
		double duanxinFee=0;
		double tonghuaFee=0;
		if(jiluList!=null) {
		for(JiLu jilu:jiluList) {
			
			if(jilu.getUsedliuliang()>newTaocan.getLiuliang()) {
				liuliangFee=(jilu.getUsedliuliang()-newTaocan.getLiuliang())*0.1;
				
			}
			if(jilu.getUsedtime()>newTaocan.getTonghua()) {
				tonghuaFee=(jilu.getUsedtime()-newTaocan.getTaocan())*0.2;
				
			}
			if(jilu.getUsedfmcount()>newTaocan.getDuanxin()) {
				duanxinFee=(jilu.getUsedfmcount()-newTaocan.getDuanxin())*0.1;
				
			}
			
			
		}
		totalConsumption+=liuliangFee+duanxinFee+tonghuaFee;
		user.setLiuliangFee(liuliangFee);
		user.setTonghuaFee(tonghuaFee);
		user.setDuanxinFee(duanxinFee);
		
		 user.setMoney(user.getMoney()-newTaocan.getYuezifei());
		 int updateResult=userMapper.updateUser(user);
		 if(updateResult>0) {
			 return (int) totalConsumption;
			 
		 }else {
			 return 0;
		 }
		}

		return newTaocanId;
		
	}


    
}
